package com.me.videoservice.controller;

import com.me.videoservice.model.callback.CloseCallBack;
import com.me.videoservice.model.callback.ConnectCallBack;
import com.me.videoservice.model.callback.DvrCallBack;
import com.me.videoservice.model.callback.HlsCallBack;
import com.me.videoservice.model.callback.PlayCallBack;
import com.me.videoservice.model.callback.PublishCallBack;
import com.me.videoservice.model.callback.StopCallBack;
import com.me.videoservice.model.callback.UnpublishCallBack;
import com.me.videoservice.model.common.CallbackRetCode;
import com.me.videoservice.model.common.StreamApp;
import com.me.videoservice.service.VideoConnectService;
import com.me.videoservice.service.VideoM3u8Service;
import com.me.videoservice.service.VideoPublishService;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/")
@CrossOrigin
@Slf4j
public class CallBackController {

  @Autowired
  private VideoPublishService videoPublishService;

  @Autowired
  private VideoM3u8Service videoM3u8Service;
  @Autowired
  private VideoConnectService videoConnectService;

  @PostMapping("on_connect")
  Integer onConnect(@RequestBody ConnectCallBack callback) {
    return Arrays.stream(StreamApp.values()).noneMatch(it -> it.getCode().equals(callback.getApp()))
        ? CallbackRetCode.NO_APP.getCode()
        : videoConnectService.checkClientAndStream(callback.getClientId(), null);
  }

  @PostMapping("on_close")
  Integer onClose(@RequestBody CloseCallBack callback) {
    log.debug("on_close ==========$callback");
    return 0;
  }

  @PostMapping("on_publish")
  Integer onPublish(@RequestBody PublishCallBack callback) {
    log.debug("on_publish ==========$callback");
    return videoPublishService.publish(callback);
  }

  @PostMapping("on_unpublish")
  Integer onUnpublish(@RequestBody UnpublishCallBack callback) {
    log.debug("on_unpublish ==========$callback");
    videoPublishService.unPublish(callback);
    return 0;
  }

  @PostMapping("on_play")
  Integer onPlay(@RequestBody PlayCallBack callback) {
    log.debug("on_play ==========$callback");
    return 0;
  }

  @PostMapping("on_stop")
  Integer onStop(@RequestBody StopCallBack callback) {
    log.debug("on_stop ==========$callback");
    return 0;
  }

  @PostMapping("on_dvr")
  Integer onDvr(@RequestBody DvrCallBack callback) {
    log.debug("on_dvr ==========$callback");
    return 0;
  }

  @PostMapping("on_hls")
  Integer onHls(@RequestBody HlsCallBack callback) {
    log.debug("on_hls ==========$callback");
    videoM3u8Service.hls(callback);
    return 0;
  }

  @GetMapping("on_hls_notify/{app}/{stream}/{ts_url}/{param}")
  Integer onHlsNotify(@PathVariable String app,
      @PathVariable String stream,
      @PathVariable("ts_url") String tsUrl,
      @PathVariable String param) {
    // HlsNotifyCallBack(app, stream, param, tsUrl)
    log.debug("on_hls_notify ==========$callback");
    return 0;
  }

}
